package br.com.tamochunas.controlefinanceiro.model.DAO;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.tamochunas.controlefinanceiro.model.entity.Lancamento;

public class LancamentoDAO extends GenericDAO {
	private static Logger LOGGER = Logger.getLogger(LancamentoDAO.class);

	public Lancamento get(Long id){
		LOGGER.debug("Buscando objeto pelo ID: " +  id);
		Session session = getSession();
		transaction = session.beginTransaction();
		
		try{
			Criteria criteria = session.createCriteria(Lancamento.class);
			criteria.add(Restrictions.idEq(id));
			return (Lancamento) criteria.uniqueResult();
		}
		catch(Throwable ex){
			LOGGER.error("Erro ao buscar o registro ID: " + id, ex);
			throw new RuntimeException();			
		}
		finally{
			session.close();
		}		
	}

	public Collection<Lancamento> getLista(){
		LOGGER.debug("Buscando lista de objetos");
		Session session = getSession();
		transaction = session.beginTransaction();
		
		try{
			return session.createCriteria(Lancamento.class).list();
		}
		catch(Throwable ex){
			LOGGER.error("Erro ao listar objetos: ", ex);
			throw new RuntimeException();			
		}	
		finally{
			session.close();
		}		
	}
}
